<?php
/**
 * 缓存类
 * @author zengnz
 *
 */

class CacheService
{
	protected $cache = '';
	
	private $_Model = '';
	
	private $_cacheTable = '';
	
	// 公共缓存前缀
	const COMMON_CACHE = 'COMMONCACHE:';
	// 配置信息缓存
	const INIT_CACHE = 'INITCACHE:';
	// 用户缓存
	const USER_CACHE = 'USERCACHE:';
	
	// 初始化
	public function init($type='mem', $timeout=false, $persistent=false) {
		$this -> _Model = new Model();
	
		switch ($type) {
			case 'mem':
				$this -> _cacheTable = 'mem_cache_keys';
				$this -> cache = Cache::getInstance('Memcache',array('host'=>C('MEM_HOST'), 'port'=>C('MEM_PORT'), 'timeout'=>$timeout, 'persistent'=>$persistent));
				break;
			default:
				$this -> cache = '';
		}
	}
	
	public function set($key, $val) {
		$this -> cache -> set($key, $val);		
	}
	
	public function get($key) {
		return $this -> cache -> get($key);
	}
	
	public function rm($key) {
		$this -> cache -> rm($key);		
	}
	
	/**
	 * 保存KEY到setting 表
	 * @param string $key
	 * @param string $type
	 */
	public function saveKeys($key, $type='common', $uid='') {
		$keys = 'KEY:STR:';
		switch ($type) {
			case 'common':
				$keys .= self::COMMON_CACHE;
				break;
			case 'init':	
				$keys .= self::INIT_CACHE;
				break;
			case 'user':
				$keys .= self::USER_CACHE.'UID:'.$uid;
				break;	
			default:
				$keys .= self::COMMON_CACHE;	
		}
		
		$keysRecord = $this -> cache -> get($keys);
		
		if ( !$keysRecord ) {
			$keysRecord = $this -> getCacheKeys('mark="'.$keys.'"');
			
			$flag = 0;
			//数据库不存在，则保存记录
			if (!$keysRecord) {
				$keysRecord = array(
					'mark' => $keys,
					'title' => $keys.' 缓存键值',
					'value' => $key,
					'uid' => $uid
				);
				
				$flag = $this -> addCacheKeys($keysRecord);
				if (!$flag)
					return false;
			}

			// 缓存插入的信息
			$this -> cache -> set($keys, $keysRecord);
			if ($flag)
				return true;
		}

		$keysArr = explode('|', $keysRecord['value']);
		if ($keysArr && !in_array($key, $keysArr)) {
			$keyStr = implode('|', array_merge($keysArr, array($key)));
			
			$updata = array('value' => $keyStr);
			$flag = $this -> updateCacheKeys('mark = "'.$keys.'"', $updata);
			if (!$flag)
				return false;
			
			// 修改成功清除缓存
			$this -> cache -> set($keys, $updata);
		}
		
		return true;
	}
	
	/**
	 * 清除缓存
	 */
	public function clearCache($type='common', $uid='') {
		$keys = 'KEY:STR:';
		switch ($type) {
			case 'common':
				$keys .= self::COMMON_CACHE;
				break; 
			case 'init':
				$keys .= self::INIT_CACHE;
				break;
			case 'user':
				$keys .= self::USER_CACHE.'UID:'.$uid;
				break;
			default:
				$keys .= self::COMMON_CACHE;
		}
		
		$keyStr = $this -> cache -> get($keys);
		if ( !$keyStr ) {
			$keyStr = $this -> getCacheKeys('mark = "'.$keys.'"');
		}
		
		if ($keyStr) {
			$keyArr = explode('|', $keyStr['value']);
			foreach ($keyArr as $key){
				$this -> cache -> rm($key);
			}

			$flag = $this -> updateCacheKeys('mark = "'.$keys.'"', array('value'=>'null'));
			if ($flag)
				$this -> cache -> rm($keys);
		}
		
		return true;
	}
	
	/**
	 * 添加缓存键名记录
	 * @param array $data
	 */
	public function addCacheKeys($data) {
		$flag = $this -> _Model
				-> table($this->_cacheTable)
				-> add($data);

		return $flag;
	}
	
	/**
	 * 更新缓存键名记录
	 * @param string $where
	 * @param array $data
	 */
	public function updateCacheKeys($where, $data) {
		$flag = $this -> _Model
				-> table($this->_cacheTable)
				-> where($where)
				-> save($data);
		
		return $flag;
	}
	
	/**
	 *  获取缓存键名记录信息
	 * @param string $where
	 */
	public function getCacheKeys($where) {
		$info = $this -> _Model
				-> table($this->_cacheTable)
				-> where($where)
				-> find();

		return $info;
	}
	
	
}